﻿#include "pch.h"

namespace fs = std::filesystem;

// 测试固件
class MethodFileTest : public ::testing::Test
{
protected:
    void SetUp() override
    {
        // 创建测试文件
        std::ofstream("test.method") << "dummy method content";

        // 创建谱图和方法句柄
        orgHandle = createOrgFile("test_org");
        methodHandle = createMethodFile("test_method");
        ASSERT_GT(orgHandle, 0);
        ASSERT_GT(methodHandle, 0);
    }

    void TearDown() override
    {
        // 清理测试文件
        fs::remove("test.method");
        fs::remove("saved.method");

        // 关闭所有句柄
        closeMethodFile(methodHandle);
        closeOrgFile(orgHandle);
    }

    int64_t orgHandle = -1;
    int64_t methodHandle = -1;
    const int32_t validHandleType = METHOD_HANDLE;
    const int32_t validOrgHandleType = ORG_HANDLE; // 对应第一个信号通道
};

// 测试用例1：基础生命周期
TEST_F(MethodFileTest, FileLifecycle)
{
    // 创建新方法
    int64_t newHandle = createMethodFile("new_method");
    ASSERT_GT(newHandle, 0);

    // 保存方法
    EXPECT_TRUE(saveMethodFile(newHandle, "saved.method"));

    // 关闭方法
    EXPECT_TRUE(closeMethodFile(newHandle));

    // 验证关闭后操作失效
    TMethodHead head;
    EXPECT_FALSE(getMethodHead(newHandle, METHOD_HANDLE, &head));
}

// 测试用例2：无效句柄操作
TEST_F(MethodFileTest, InvalidHandleOperations)
{
    const int64_t invalidHandle = 999;
    TMethodHead head;

    // 测试方法句柄
    EXPECT_FALSE(getMethodHead(invalidHandle, METHOD_HANDLE, &head));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_METHOD_HANDLE_NOT_EXIST);

    // 测试谱图句柄
    EXPECT_FALSE(getMethodHead(invalidHandle, ORG_HANDLE, &head));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_ORG_HANDLE_NOT_EXIST);
}

// 测试用例3：句柄类型验证
TEST_F(MethodFileTest, HandleTypeValidation)
{
    TMethodHead head;

    // 无效类型下限
    EXPECT_FALSE(getMethodHead(methodHandle, METHOD_HANDLE - 1, &head));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_IPARAM_VALUE_INVALID);

    // 无效类型上限
    EXPECT_FALSE(getMethodHead(orgHandle, ORG_HANDLE + 1, &head));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_IPARAM_VALUE_INVALID);

    // 类型与方法不匹配
    EXPECT_FALSE(getMethodHead(methodHandle, ORG_HANDLE, &head));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_ORG_HANDLE_NOT_EXIST);
}

// 测试用例4：基础数据操作
TEST_F(MethodFileTest, MethodHeadOperations)
{
    TMethodHead original, modified;

    // 获取原始数据
    ASSERT_TRUE(getMethodHead(methodHandle, METHOD_HANDLE, &original));

    // 修改数据
    std::string test_str = "modified_method";
    char *pSource = (char *)test_str.c_str();
    std::copy(pSource, pSource + MIN(strlen(pSource) + 1, sizeof(modified.Name)), modified.Name);
    modified.Name[sizeof(modified.Name) - 1] = 0;

    modified.Version = original.Version + 1;

    // 设置并验证
    EXPECT_TRUE(setMethodHead(methodHandle, METHOD_HANDLE, &modified));

    TMethodHead current;
    EXPECT_TRUE(getMethodHead(methodHandle, METHOD_HANDLE, &current));
    EXPECT_STREQ(current.Name, "modified_method");
}

// 测试用例5：事件表操作
TEST_F(MethodFileTest, EventListOperations)
{
    // 准备测试数据
    TEventList events[2] = {{1.0, 1.0, NOEVENT, 100, true, 0, 1}, {2.0, 2.0, NOEVENT, 200, false, 0, 2}};

    // 设置方法事件
    EXPECT_TRUE(setMethodEventList(methodHandle, METHOD_HANDLE, events, 2));

    // 获取事件数据
    TEventList buffer[2];
    int32_t count = 0;
    EXPECT_TRUE(getMethodEventList(methodHandle, METHOD_HANDLE, buffer, 2, &count));

    // 验证数据
    EXPECT_EQ(count, 2);
    EXPECT_EQ(buffer[0].event, NOEVENT);
    EXPECT_FLOAT_EQ(buffer[1].ts, 2.0);
}

// 测试用例6：谱图集成操作
TEST_F(MethodFileTest, OrgIntegrationOperations)
{
    // 准备测试数据
    TCompList comps[1] = {{1.0, 2.0, 3.0, 4.0, 5.0, 1, 2, 6.0, 7.0, ON_AREA, 3, "C6H12O6", 0, 0, 0, 0, 0, 0, 1}};

    // 设置谱图方法数据
    EXPECT_TRUE(setMethodCompList(orgHandle, ORG_HANDLE, comps, 1));

    // 获取谱图方法数据
    TCompList buffer[1];
    int32_t count = 0;
    EXPECT_TRUE(getMethodCompList(orgHandle, ORG_HANDLE, buffer, 1, &count));

    // 验证数据
    EXPECT_EQ(count, 1);
    EXPECT_STREQ(buffer[0].Name, "C6H12O6");
}

// 测试用例7：缓冲区不足测试
TEST_F(MethodFileTest, InsufficientBuffer)
{
    // 准备测试数据
    TGroupList groups[3] = {{1}, {2}, {3}};
    ASSERT_TRUE(setMethodGroupList(methodHandle, METHOD_HANDLE, groups, 3));

    // 尝试获取到小缓冲区
    TGroupList smallBuffer[2];
    int32_t count = 0;
    EXPECT_FALSE(getMethodGroupList(methodHandle, METHOD_HANDLE, smallBuffer, 2, &count));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_BUFFER_INSUFFICIENT);
    EXPECT_EQ(count, 3);
}

// 测试用例8：文件异常处理
TEST_F(MethodFileTest, FileExceptionHandling)
{
    // 打开不存在的文件
    int64_t handle = openMethodFile("nonexistent.method");
    EXPECT_EQ(handle, -1);
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_FILE_NOT_EXISTS);

    // 保存到只读目录
    fs::create_directory("readonly_dir");
    fs::permissions("readonly_dir", fs::perms::owner_read, fs::perm_options::replace);
#ifdef _WIN32
    EXPECT_TRUE(saveMethodFile(methodHandle, "readonly_dir/test.method"));
    EXPECT_EQ(getLastErrorCode(), 0);
#else
    EXPECT_FALSE(saveMethodFile(methodHandle, "readonly_dir/test.method"));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_FILE_OPEN_FAILED);
#endif

    fs::remove_all("readonly_dir");
}

// 测试用例9：边界值测试
TEST_F(MethodFileTest, BoundaryValueTesting)
{
    // 测试最大数据量
    std::vector<TCalibList> maxData(MAX_CALIBRATIONS);
    EXPECT_TRUE(setMethodCalibList(methodHandle, METHOD_HANDLE, maxData.data(), MAX_CALIBRATIONS));

    // 测试超过最大值
    std::vector<TCalibList> overflow(MAX_CALIBRATIONS + 1);
    EXPECT_FALSE(setMethodCalibList(methodHandle, METHOD_HANDLE, overflow.data(), MAX_CALIBRATIONS + 1));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_IPARAM_VALUE_INVALID);

    // 测试空数据
    EXPECT_FALSE(setMethodCalibList(methodHandle, METHOD_HANDLE, nullptr, 0));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_PPARA_IS_NULL);
}

// 测试用例10：多通道隔离测试
TEST_F(MethodFileTest, ChannelIsolation)
{
    // 设置不同通道的数据
    TSegmentList seg1 = {1, 5};
    TSegmentList seg2 = {2, 10};

    EXPECT_TRUE(setMethodSegmentList(orgHandle, ORG_HANDLE, &seg1, 1));

    // 验证数据隔离
    TSegmentList buffer;
    int32_t count = 0;
    EXPECT_TRUE(getMethodSegmentList(orgHandle, ORG_HANDLE, &buffer, 1, &count));
    EXPECT_EQ(count, 1);
    EXPECT_EQ(buffer.SegmentMode, 1);
}

// 测试用例11：空指针参数检查
TEST_F(MethodFileTest, NullPointerCheck)
{
    // get函数空指针
    EXPECT_FALSE(getMethodHead(methodHandle, METHOD_HANDLE, nullptr));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_PPARA_IS_NULL);

    // set函数空指针
    EXPECT_FALSE(setMethodHead(methodHandle, METHOD_HANDLE, nullptr));
    EXPECT_EQ(getLastErrorCode(), ERR_CODE_PPARA_IS_NULL);

    // 数据操作空指针
    int32_t count = 0;
    EXPECT_FALSE(getMethodEventList(methodHandle, METHOD_HANDLE, nullptr, 1, &count));
    EXPECT_FALSE(getMethodEventList(methodHandle, METHOD_HANDLE, (TEventList *)&count, 1, nullptr));
}

// 测试用例12：校准结果操作
TEST_F(MethodFileTest, CalibrationResultOperations)
{
    // 准备测试数据
    TCalibResult result{};
    result.f0 = 1.234;
    result.f1 = 5678.9;

    // 设置校准结果
    EXPECT_TRUE(setMethodCalibResult(methodHandle, METHOD_HANDLE, &result, 1));

    // 获取校准结果
    TCalibResult buffer;
    int32_t count = 0;
    EXPECT_TRUE(getMethodCalibResult(methodHandle, METHOD_HANDLE, &buffer, 1, &count));

    // 验证数据
    EXPECT_DOUBLE_EQ(buffer.f0, 1.234);
    EXPECT_DOUBLE_EQ(buffer.f1, 5678.9);
}
#ifdef TEST_METHOD
int32_t main(int32_t argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
#endif